jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces.ProvidesConsumesInterfaces", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.prototype.serviceId;
        	plugin.prototype.nameSuffix = "";
        	plugin.prototype.selectedInterfaceId;
        
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	
        	plugin.interfaceSelectedSubject = "com.tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces.ProvidesConsumesInterfaces.interfaceSelected";
        	
        	plugin.serviceInterfacesTableCacheId = "SPM_SERVICE_INTERFACES_TABLE_CACHE";
        	plugin.interfaceReferencesTableCacheId = "SPM_INTERFACE_REFERENCES_TABLE_CACHE";
        	
        	plugin.prototype.isServiceInterfaceMode;
        	
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(nameSuffix, isServiceInterfaceMode, serviceId) { 
				this.nameSuffix = nameSuffix;
				this.selectedInterfaceId = null;
				var providesTitle = this.getDescendantOfName("titleLabel");
				providesTitle.setName(providesTitle.getName() + nameSuffix);
                var providesTable = this.getDescendantOfName("interfacesTable");
				providesTable.setName(providesTable.getName() + nameSuffix);
                var pcHelpButton = this.getDescendantOfName("pcHelpButton");
				pcHelpButton.setName(pcHelpButton.getName() + nameSuffix);
				
				if(serviceId)
				{ this.serviceId = serviceId; }
				
            	this.isServiceInterfaceMode = isServiceInterfaceMode;
            	tibco.admin.monitormanage.general.PollingHandler.register(tibco.admin.monitormanage.general.PollingHandler.GENERAL_POLLING_HANDLER_ID, this, this.updateComponent, tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId);
            	tibco.admin.monitormanage.general.RefreshHandler.register(this, this.updateComponent, tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.providesConsumes.ProvidesConsumes.getServiceIdSubject, this.handleGetServiceId, this);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.selectedTimeframeSubject, this.timeframeSelected, this);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.providesConsumes.ProvidesConsumes.handleInitialSelectSubject, this.handleInitialSelect, this);
            	this.callGetProvidesConsumesInterfaces();
                this.getDescendantOfName("pcHelpButton" + this.nameSuffix).setEvent("this.getAncestorOfName('interfacesBlock').pcHelpButtonClicked(objEVENT)", "jsxexecute");
            }

           plugin.prototype.pcHelpButtonClicked = function (objEVENT){
               pcHelpObj= this.getDescendantOfName("pcHelpButton" + this.nameSuffix);

               if(this.isServiceInterfaceMode){
                 pcHelpObj.openHelp("MonitorManage.Service.Integrated.Provides", "tibco.admin", objEVENT); }
               else
               pcHelpObj.openHelp("MonitorManage.Service.Integrated.Consumes", "tibco.admin", objEVENT);;
           }
			
            plugin.prototype.handleInitialSelect = function(subject, message, providesConsumesServicesObj) {
				var isInterfaceConsumersMode = message.isInterfaceConsumersMode;
				if( (!isInterfaceConsumersMode && providesConsumesServicesObj.isServiceInterfaceMode) ||
				   (isInterfaceConsumersMode && !providesConsumesServicesObj.isServiceInterfaceMode))
				{ providesConsumesServicesObj.selectedInterfaceId = message.consumerProviderKey; }
				else
				{
					// clear the selected item
					providesConsumesServicesObj.selectedInterfaceId = null;
				}
				providesConsumesServicesObj.updateComponent();
			}
			
			plugin.prototype.updateComponent = function() {
				this.callGetProvidesConsumesInterfaces();
			}
			
			plugin.prototype.handleGetServiceId = function(subject, message, tableObj) {
				tableObj.serviceId = message.serviceId;
				tableObj.selectedInterfaceId = null;
				tableObj.callGetProvidesConsumesInterfaces();
			}
			
			plugin.prototype.timeframeSelected = function(subject, message, tableObj) {
            	var timeframeId = message.selectedTimeframe;
            	tableObj.callGetProvidesConsumesInterfaces(timeframeId);
            }
			
			plugin.prototype.interfaceSelected = function(interfaceIdVal) {
				this.selectedInterfaceId = interfaceIdVal;
				var message = {isServiceInterfaceMode:this.isServiceInterfaceMode, interfaceId:interfaceIdVal};
				tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.interfaceSelectedSubject, message);
			}
			
			//call this method to begin the service call (eg.service.callgetServiceInterfaces();)
    		plugin.prototype.callGetProvidesConsumesInterfaces = function(timeframeId) {
    			var rulesPath;
    			var dataPath;
    			var operation;
    			if(this.isServiceInterfaceMode)
    			{ 
    				rulesPath = "tibco/admin/monitormanage/uicomponents/table/providesConsumesInterfaces/rules/serviceInterfacesRules.xml"; 
    				dataPath = "tibco/admin/monitormanage/uicomponents/table/providesConsumesInterfaces/data/serviceInterfacesData.xml"; 
    				operation = "getServiceInterfaces";
    			}
    			else
    			{ 
    				rulesPath = "tibco/admin/monitormanage/uicomponents/table/providesConsumesInterfaces/rules/interfaceReferencesRules.xml"; 
    				dataPath = "tibco/admin/monitormanage/uicomponents/table/providesConsumesInterfaces/data/interfaceReferencesData.xml"; 
    				operation = "getInterfaceReferences";
    			}

    			if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), rulesPath, operation);
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL(dataPath);
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), rulesPath), operation, null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), dataPath));
                }
                
                objService.providesConsumesInterfacesObj = this;
                objService.serviceId = this.serviceId;
                if(!timeframeId)
				{ timeframeId = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeId(); }
				objService.timeframeId = timeframeId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("portType");
				metricsList.add("faults");
				metricsList.add("successes");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetProvidesConsumesInterfacesSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetProvidesConsumesInterfacesError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetProvidesConsumesInterfacesInvalid);
      			objService.doCall();
   	 		};

    		plugin.prototype.ongetProvidesConsumesInterfacesSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			
      			var providesConsumesInterfacesObj = objEvent.target.providesConsumesInterfacesObj;
      			
      			// update the cache with the proper attributes for the table to render
      			var cacheId;
      			if(providesConsumesInterfacesObj.isServiceInterfaceMode)
      			{ cacheId = plugin.serviceInterfacesTableCacheId; }
      			else
      			{ cacheId = plugin.interfaceReferencesTableCacheId; }
      			var cacheDoc = tibco.admin.Main.getServer().Cache.getDocument(cacheId);
      			var interfaceRecords = cacheDoc.getChildNodes();
      			for(i = 0; i < interfaceRecords.size(); i++)
      			{
      				var interfaceRecord = interfaceRecords.get(i);
      				var faults = interfaceRecord.getAttribute("faults");
      				var successes = interfaceRecord.getAttribute("successes");
      				var faultSuccessRatio = faults + "/" + successes;
      				var faultSuccessRatioValue = faults/successes;
      				interfaceRecord.setAttribute("faultSuccessRatio",faultSuccessRatio);
      				interfaceRecord.setAttribute("faultSuccessRatioValue",faultSuccessRatioValue);
      				var hits = (faults - 0) + (successes - 0);
      				interfaceRecord.setAttribute("hits", hits);
      			}
      			
      			var tableObj = providesConsumesInterfacesObj.getDescendantOfName("interfacesTable" + providesConsumesInterfacesObj.nameSuffix);
      			tableObj.setXMLId(cacheId);
      			
      			if(providesConsumesInterfacesObj.selectedInterfaceId)
      			{ 
      				tableObj.selectRecord(providesConsumesInterfacesObj.selectedInterfaceId); 
      				providesConsumesInterfacesObj.interfaceSelected(providesConsumesInterfacesObj.selectedInterfaceId);
      			}
      			tableObj.repaintData();
    		};

    		plugin.prototype.ongetProvidesConsumesInterfacesError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			//jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetProvidesConsumesInterfacesInvalid = function(objEvent) {
      			//jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};
            
        });